package com.wing.product.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseFeeItems;
import com.wing.product.model.request.WarehouseFeeItemsForm;
import com.wing.product.model.response.WarehouseFeeItemsVO;
import com.wing.product.service.WarehouseFeeItemsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "收费项管理", tags = "#/warehouse/product/warehouseFeeItems")
@RestController("WarehouseFeeItemsController")
@RequestMapping("/warehouse/feeItems")
@RequiredArgsConstructor
public class WarehouseFeeItemsController {

    private final WarehouseFeeItemsService warehouseFeeItemsService ;


    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseFeeItemsVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "999", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            String feeItemCode
    ) {
//        if (JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)) {
//            customerId = JwtUtils.getDepartmentId();
//        }
        Page<WarehouseFeeItems> pg = new Page<>(page, limit);
        Map paramMap = new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("feeItemCode", feeItemCode);
        //搜索
        IPage<WarehouseFeeItemsVO> list = warehouseFeeItemsService.pageList4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加收费项", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(WarehouseFeeItemsForm warehouseFeeItemsForm)  {
        try{
            return warehouseFeeItemsService.saveWarehouseFeeItems(warehouseFeeItemsForm);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "修改收费项", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PutMapping("/update")
    public JsonResult update(WarehouseFeeItemsForm warehouseFeeItemsForm)  {
        try{
            return warehouseFeeItemsService.updateWarehouseFeeItems(warehouseFeeItemsForm);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查询收费明细", notes = "查询")
    @GetMapping ("/selectFeeItems")
    public JsonResult<WarehouseFeeItems> selectFeeItems(@RequestParam String id){
        if (!StringUtils.isEmpty(id)){
            WarehouseFeeItems warehouseFeeItems = warehouseFeeItemsService.selectFeeItems(id);
            if (ObjectUtil.isNotEmpty(warehouseFeeItems)){
                return  JsonResult.success("请求成功",warehouseFeeItems);
            }else {
                return  JsonResult.success("数据为空",warehouseFeeItems);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "删除收费明细", notes = "查询")
    @GetMapping ("/deleteFeeItems")
    public JsonResult deleteFeeItems(@RequestParam String id,@RequestParam String status){
        return  warehouseFeeItemsService.deleteFeeItems(id,status);
    }
}
